import matplotlib as mpl
import matplotlib.pyplot as plt
# from PIL import Image

plt.rcParams['text.usetex'] = True
plt.rc('text.latex', preamble=r'\usepackage[cm]{sfmath}')

mpl.rcParams['font.family'] = 'serif'
mpl.rcParams['font.serif'] = 'Palatino'
mpl.rcParams['font.size'] = 18

mpl.rcParams['legend.frameon'] = True
mpl.rcParams['legend.fontsize'] = 18
mpl.rcParams['legend.labelspacing'] = 0.4
mpl.rcParams['legend.handlelength'] = 1.8
mpl.rcParams['legend.borderaxespad'] = 0.25
mpl.rcParams['legend.handletextpad'] = 0.5
mpl.rcParams['legend.borderpad'] = 0.3

mpl.rcParams['lines.linewidth'] = 2.25
mpl.rcParams['axes.grid'] = True
mpl.rcParams['grid.alpha'] = 0.5
mpl.rcParams['axes.linewidth'] = 1.6

mpl.rcParams["savefig.bbox"] = 'tight'
mpl.rcParams["savefig.pad_inches"] = 0.05

mpl.rcParams['savefig.transparent'] = True
mpl.rcParams['figure.autolayout'] = True

figsize_standard = (6.3, 4.7)
figsize_triple = (4.2, 4.7)

format_list = ['pdf']


def save_figure(fig, filename, folder='figures/', close=True, show_plot=True, format_list=format_list, dpi=500, print_style=False):
    for format in format_list:
        fig.savefig(folder + filename + '.' + format, format=format, dpi=dpi)
    # if print_style:
    #     fig.savefig(folder + filename + '.tiff', format='tiff', dpi=max(dpi, 300))
    #     image_color = Image.open(folder + filename + '.tiff').convert('L')
    #     image_color.save(folder + filename + '.tiff')
    if close is True:
        if show_plot is True:
            plt.show()
        plt.close(fig)
    return
